package ee.ttu.agent.smartHouse.gui;



import jade.gui.GuiEvent;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import ee.ttu.agent.smartHouse.util.CustomEventTypes;
import ee.ttu.agent.smartHouse.util.IOnlineInteractor;



public class OnlineInteractionGui extends JFrame implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7252384279252456549L;

	

	// GUI stuff
	private JTextArea enteredText = new JTextArea(10, 32);
	private JTextField typedText = new JTextField(32);

	private IOnlineInteractor myAgent;

	@SuppressWarnings("deprecation")
	public OnlineInteractionGui(IOnlineInteractor parentAgent) {
		myAgent = parentAgent;
		this.locate(200, 600);
		
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
			}
		});
		
		String parentId = myAgent.getParentId();
		
		// create GUI stuff
		enteredText.setEditable(true);
		enteredText.setBackground(Color.LIGHT_GRAY);
		typedText.addActionListener(this);

		Container content = getContentPane();
		content.add(new JScrollPane(enteredText), BorderLayout.CENTER);
		content.add(typedText, BorderLayout.SOUTH);

		// display the window, with focus on typing box
		setTitle(parentId + " Online Interaction Mockup");
		pack();
		
		typedText.requestFocusInWindow();
		setVisible(true);
		
		
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(WindowEvent winEvt) {
				myAgent.disposeOnlineInteractor();
			}
		});
	}

	// process TextField after user hits Enter
	public void actionPerformed(ActionEvent e) {		
		String text = typedText.getText();
		enteredText.append(text + "\n");
		sendTextToAgent(text);
		
		typedText.setText("");
		typedText.requestFocusInWindow();
	}
	
	public void setTypedText(String textToAppend){
		enteredText.append(textToAppend + "\n");
	}
	
	public void sendTextToAgent(String outgouingMessage){
		// send start online conversation message
					GuiEvent ge = new GuiEvent(this, CustomEventTypes.OI_OUTBOUND);
					ge.addParameter(outgouingMessage);
					myAgent.postGuiEvent(ge);
		}
}
